

// raw data to buffer
var result = Buffer.alloc(10);
var data = 0x30401020

for (var i = 0; i < 4; i++) {
  result.writeUInt8(0xff & (data >> (i * 8)), i);
}

console.log(Buffer.from([0x0202,0x0210]))
console.log(Buffer.from([0x02, 0x02, 0x10, 0x02]))
formatBuffer(result)
    /**
   * 测试
   * @param {Buffer} buff
   * @return {Buffer} result
   */
function formatBuffer(buff){
  var nBuff = Buffer.allocUnsafe(buff.length)
  var offset = 0
  for(var i =0; i<buff.length; i++){
   // targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number
    buff.copy(nBuff, buff.length - offset - 1, offset, offset+ 1)
    offset ++
  }
  console.log(nBuff.toString('hex'))
}